<?php
/**
 * Created by PhpStorm.
 * User: syskey
 * Date: 2018/9/7
 * Time: 17:39
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Common\CommonController;
use App\Http\Controllers\Common\WriteConfigController;
use App\Http\Controllers\Controller;
use App\Larqq;
use App\Link;
use App\User;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

class ActionController extends Controller
{
    private $writeconfig;
    private $link;
    private $user;
    private $qq;
    private $client;
    private $common;

    public function __construct()
    {
        $this->writeconfig = new WriteConfigController();
        $this->link = new Link();
        $this->user = new User();
        $this->qq = new Larqq();
        $this->client = new Client();
        $this->common = new CommonController();
    }

    public function ClearCache()
    {
        $msg = Cache::flush();
        if ($msg) {
            return ['status' => 200, 'msg' => '清理成功'];
        } else {
            return ['status' => 400, 'msg' => '清理失败'];
        }
    }

    public function DeleteData(Request $request)
    {
        $sid = $request->post('sid');
        $mb = $request->post('mb');
        $msg = $this->$mb->destroy($sid);
        if ($msg) {
            return ['status' => 200, 'msg' => '删除成功'];
        } else {
            return ['status' => 400, 'msg' => '删除失败'];
        }
    }

    public function WConfig(Request $request)
    {
        $config = $request->post();
        $configname = $config['configname'];
        unset($config['configname']);
        $path = CONFIG_PATH . $configname . '.php';
        $res = $this->writeconfig->writeConfig($config, $path);
        if ($res == "ok") {
            return ['status' => 200, 'msg' => '修改成功'];
        } else {
            return ['status' => 400, 'msg' => '修改失败'];
        }
    }

    public function AddLink(Request $request)
    {
        $linkres = $request->post();
        foreach ($linkres as $key => $v) {
            $this->link->$key = $v;
        }
        $msg = $this->link->save();
        if ($msg) {
            return ['status' => 200, 'msg' => '添加成功'];
        } else {
            return ['status' => 400, 'msg' => '添加失败'];
        }
    }

    public function EditLink(Request $request)
    {
        $linkres = $request->post();
        $id = $linkres['lar_link_id'];
        unset($linkres['lar_link_id']);
        $msg = $this->link->where(['lar_link_id' => $id])->update($linkres);
        if ($msg) {
            return ['status' => 200, 'msg' => '修改成功'];
        } else {
            return ['status' => 400, 'msg' => '修改失败'];
        }

    }

    public function AddQq(Request $request)
    {
        $qqres = $request->post();
        foreach ($qqres as $key => $v) {
            $this->qq->$key = $v;
        }
        $msg = $this->qq->save();
        if ($msg) {
            return ['status' => 200, 'msg' => '添加成功'];
        } else {
            return ['status' => 400, 'msg' => '添加失败'];
        }
    }

    public function EditQq(Request $request)
    {
        $qqres = $request->post();
        $id = $qqres['lar_qq_id'];
        unset($qqres['lar_qq_id']);
        $msg = $this->qq->where(['lar_qq_id' => $id])->update($qqres);
        if ($msg) {
            return ['status' => 200, 'msg' => '修改成功'];
        } else {
            return ['status' => 400, 'msg' => '修改失败'];
        }

    }

    public function ChangeInfo(Request $request)
    {
        $adminname = $request->post('name');
        $adminpassword = $request->post('password');
        $nowadminname = Auth::user()->name;
        $msg = $this->user->where(['name' => $nowadminname])->update(['name' => $adminname, 'password' => bcrypt($adminpassword)]);
        if ($msg) {
            return ['status' => 200, 'msg' => '修改成功'];
        } else {
            return ['status' => 400, 'msg' => '修改失败'];
        }
    }

    public function Update(Request $request)
    {
        $type = $request->post('updatetype');
        $url = "http://update.laracvs.com/" . $type . ".zip";
        try {
            $data = $this->client->get($url)->getBody()->getContents();
            $filename = $type . ".zip";
            $res = Storage::disk('local')->put($filename, $data);
            if ($res) {
                $flag = $this->common->unzip(storage_path("update")."/" . $type . ".zip", CONFIG_PATH, true, true);
                if ($flag) {
                    return ['status' => 200, 'msg' => "更新成功"];
                } else {
                    return ['status' => 200, 'msg' => "更新失败"];
                }
            } else {
                return ['status' => 500, 'mag' => '文件保存失败'];
            }

        } catch (RequestException $exception) {
            return ['status' => 500, 'mag' => '文件下载失败'];
        }

    }

}